/**
 * 
 */
package com.jysz.certificate.persistence.model.system;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.OrderBy;

import com.jysz.certificate.persistence.enums.OrganizationEnum;
import com.jysz.webcore.ssh.entity.AbstractBaseEntity;

/**
 * 组织架构
 * 
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Entity
@Table(name = "sys_organization_")
@org.hibernate.annotations.Entity
public class Organization extends AbstractBaseEntity<Long>{

	private static final long serialVersionUID = 1L;

	private String code;
	private String name;
	private OrganizationEnum type;
	private Organization parent;
    private Integer grade; //等级  
	private Integer order; //同一级中的顺序
	private List<Organization> childs;
	private Long objectId;

	@Column(name = "_code", unique = true,columnDefinition="varchar(255)")
    public String getCode() {
		return code;
	}
    
	public void setCode(String code) {
		this.code = code;
	}
	
	@Column(name = "_name",columnDefinition="varchar(255)")
	public String getName() {
		return name;
	}
	 
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name = "_type",columnDefinition="int(11)")
	public OrganizationEnum getType() {
		return type;
	}
	
	public void setType(OrganizationEnum type) {
		this.type = type;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)  
	@JoinColumn(name = "_parent_id",columnDefinition="bigint(20)") 
	public Organization getParent() {
		return parent;
	}
	public void setParent(Organization parent) {
		this.parent = parent;
	}
	
	@Column(name = "_grade",columnDefinition="int(11)")
	public Integer getGrade() {
		return grade;
	}
	public void setGrade(Integer grade) {
		this.grade = grade;
	}
	
	@Column(name="_order",columnDefinition="int(11)")
	public Integer getOrder() {
		return order;
	}
	public void setOrder(Integer order) {
		this.order = order;
	}

	@OneToMany(targetEntity = Organization.class, cascade = { CascadeType.ALL }, mappedBy = "parent")  
	@Fetch(FetchMode.SUBSELECT)  
	@OrderBy(clause = "order")
	public List<Organization> getChilds() {
		return childs;
	}

	public void setChilds(List<Organization> childs) {
		this.childs = childs;
	}

	@Column(name="_object_id",columnDefinition="bigint(20)")
	public Long getObjectId() {
		return objectId;
	}

	public void setObjectId(Long objectId) {
		this.objectId = objectId;
	}

	
	@Transient
	public List<Organization> getStaffChilds() {
		
		if(this.getChilds() == null){
			return null;
		}
		
		List<Organization> orgs = new ArrayList<Organization>();
		for (Iterator<Organization> iterator = this.getChilds().iterator(); iterator.hasNext();) {
			Organization org =  iterator.next();
			if(OrganizationEnum.STAFF.equals(org.getType())){
				orgs.add(org);
			}
		}
		return orgs;
	}
	
	@Transient
	public List<Organization> getShopChilds() {
		if(this.getChilds() == null){
			return null;
		}
		
		List<Organization> orgs = new ArrayList<Organization>();
		for (Iterator<Organization> iterator = this.getChilds().iterator(); iterator.hasNext();) {
			Organization org =  iterator.next();
			if(OrganizationEnum.SHOP.equals(org.getType())){
				orgs.add(org);
			}
		}
		return orgs;
	}
	
	/**begin properties column constan**/
	public static final String COLUMN_ORDER = "order";
	public static final String COLUMN_GRADE = "grade";
	public static final String COLUMN_CODE = "code";
	public static final String COLUMN_PARENT = "parent";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_TYPE = "type";
	public static final String COLUMN_OBJECT_ID = "objectId";
	/**end properties column constan**/

	

	
}
